﻿module Sustainalytics.Research {

    export class CompanyResearchController {

        // current company
        public companyNameQueryString: string;
        public company: Company.CorporateDataExtendedDto;
        constructor(
            private $rootScope: ng.IScope,
            private $scope: ng.IScope,
            private $q: ng.IQService,
            private $route: ng.route.IRouteService,
            private $location: ng.ILocationService,
            private $routeParams: ng.route.IRouteParamsService,
            private CompanyService: Company.ICompanyService,
            private MostRecentActivityService: MostRecentActivity.IMostRecentActivityService,
            private $modal: Common.IModal
            )
        {

            var ctx = this;

            ctx.init();
        }


        init(): ng.IPromise<any> {
            var ctx = this;


            // get company name from route params
            var companyName: string = ctx.$routeParams['companyName'];
            ctx.companyNameQueryString = companyName;

            // get company by name
            var companyPromise = ctx.CompanyService.getByNameUrlFriendly(companyName);

            // after obtaining the company info get also other related info
            companyPromise.success(function (company) {
                ctx.company = company;
                var mostRecentAddPromise = ctx.MostRecentActivityService.saveMostRecentActivity(ctx.company.id);

            });
            
            return companyPromise;
        }

        public openSavePortfolio = function () {
            var ctx = this;
            var modalInstance = ctx.$modal.open(
                {
                    templateUrl: '/Views/Research/Common/savePortfolio_dialog.tpl.html',
                    controller: 'CompanyAddToPortfolioController',
                    size: 'lg',
                    backdrop: 'static',
                    resolve: {
                        companyId: function () {
                            return ctx.company.id;
                        },
                        companyText: function () {
                            var companyText = ctx.company.companyName;
                            if (ctx.company.exchangeCode != null && ctx.company.ticker != null)
                                companyText += " (" + ctx.company.exchangeCode + ":" + ctx.company.ticker + ")";
                            return companyText;
                        }
                    }
                });

            modalInstance.result.then((item) => {
            });
        };
    }

    export var companyResearchControllerName = 'CompanyResearchController';
    angular.module(researchModuleName).controller(companyResearchControllerName, CompanyResearchController);
} 